package xin.marcher.module.quality.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.enums.quality.QualityExceptionCode;
import xin.marcher.module.common.enums.quality.SurveyWhiteListStatusEnum;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.quality.converter.SurveyTaskWhiteListConverter;
import xin.marcher.module.quality.domain.dto.ItemQualityControlWhiteListDTO;
import xin.marcher.module.quality.domain.entity.ItemQualityControlWhitelistDO;
import xin.marcher.module.quality.domain.request.SurveyWhiteListRequest;
import xin.marcher.module.quality.repository.SurveyRepository;
import xin.marcher.module.quality.service.ItemQualityControlWhiteListService;

import java.util.Objects;

import static xin.marcher.module.common.enums.quality.QualityExceptionCode.DATA_DUPLICATION_ERROR;

/**
 *
 */
@Service
public class ItemQualityControlWhiteListServiceImpl implements ItemQualityControlWhiteListService {
    @Autowired
    private SurveyRepository surveyRepository;
    @Autowired
    private SurveyTaskWhiteListConverter whiteListConverter;

    /**
     * 查询白名单列表
     *
     * @param request
     * @return
     */
    @Override
    public PageResult<ItemQualityControlWhiteListDTO> qualityWhiteList(SurveyWhiteListRequest request) {
        checkQueryParams(request);
        return surveyRepository.queryQualityWhiteList(request);
    }

    /**
     * 变更白名单
     *
     * @param request
     * @return
     */
    @Override
    public Boolean changeQualityWhite(SurveyWhiteListRequest request) {
        checkUpdateParams(request);
        if (SurveyWhiteListStatusEnum.ADD.getCode().equals(request.getOperationType())) {
            ItemQualityControlWhitelistDO whitelistDO = surveyRepository.selectWhitelistDOOne(request);
            if (Objects.nonNull(whitelistDO)) {
                throw new BaseBizException(DATA_DUPLICATION_ERROR);
            }
            surveyRepository.addIntoQualityWhiteList(request);
            return true;
        } else if (SurveyWhiteListStatusEnum.REMOVE.getCode().equals(request.getOperationType())) {
            surveyRepository.removeFromQualityWhiteList(request);
            return true;
        }
        return false;
    }

    /**
     * 校验参数
     *
     * @param request
     */
    private void checkUpdateParams(SurveyWhiteListRequest request) {
        checkQueryParams(request);
        if (Objects.isNull(request.getOperationType())) {
            throw new BaseBizException(QualityExceptionCode.PARAM_CHECK_ERROR, QualityExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

    }

    /**
     * 校验参数是否正确
     *
     * @param request
     */
    private void checkQueryParams(SurveyWhiteListRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(QualityExceptionCode.PARAM_CHECK_ERROR, QualityExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (StringUtils.isBlank(request.getSkuId())) {
            throw new BaseBizException(QualityExceptionCode.PARAM_CHECK_ERROR, QualityExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getSellerId())) {
            throw new BaseBizException(QualityExceptionCode.PARAM_CHECK_ERROR, QualityExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getActiveFlag())) {
            throw new BaseBizException(QualityExceptionCode.PARAM_CHECK_ERROR, QualityExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }
}
